package cn.hzc.fortess.system.controller;


import cn.hzc.fortess.core.common.BizErrorEnum;
import cn.hzc.fortess.core.common.ResponseData;
import cn.hzc.fortess.core.common.annotion.RequestPermission;
import cn.hzc.fortess.core.common.exception.ServiceException;
import cn.hzc.fortess.system.entity.SettingBean;
import cn.hzc.fortess.system.page.Pagination;
import cn.hzc.fortess.system.search.SettingSearch;
import cn.hzc.fortess.system.service.SettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Objects;

@Api(tags = {"参数配置管理"})
@Controller
@RequestMapping("/sam/settings")
public class SettingController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(SettingController.class);

    private static final Integer CLZ_TYPE_SYSTEM = 1;
    private static final Integer CLZ_TYPE_CUSTOM = 2;

    @Autowired
    private SettingService settingService;

    @ApiIgnore
    @RequestMapping(value = "",method = RequestMethod.GET)
    public String index() {
        return buildToView("index.html");
    }

    @ApiIgnore
    @RequestMapping("/add")
    public String addToView() {
        SettingBean setting = new SettingBean();
        setting.setClzType(CLZ_TYPE_CUSTOM);
        setAttr("setting", setting);
        return buildToView("form.html");
    }

    @ApiOperation("新增配置")
    @RequestMapping(value = "/add.json",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData create(SettingBean entity) {
        this.settingService.add(entity);
        return ResponseData.success();
    }

    @ApiIgnore
    @RequestMapping("/edit/{id}")
    public String editToView(@PathVariable(value = "id", required = true) Integer id) {
        SettingBean setting = settingService.getById(id);
        setAttr("setting", setting);
        return buildToView("form.html");
    }

    @ApiOperation("修改配置")
    @RequestMapping(value = "/edit.json",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData edit(SettingBean entity) {
        this.settingService.update(entity);
        return ResponseData.success();
    }

    @ApiOperation("删除配置")
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData delete(@PathVariable(value = "id", required = true) Integer id) {
        SettingBean bean = settingService.getById(id);
        if (Objects.equals(bean.getClzType(), CLZ_TYPE_SYSTEM)) {
            throw new ServiceException(BizErrorEnum.CANT_DELETE_SETING);
        }
        this.settingService.delete(id);
        return ResponseData.success();
    }

    @ApiOperation("获取分页列表")
    @ResponseBody
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @RequestPermission("sam:settings:list")
    public Pagination list(SettingSearch search) {
        Pagination pagination = this.settingService.getPage(search);
        return pagination;
    }

    @Override
    protected String moduleName() {
        return "settings";
    }
}


